﻿Public Class CINNERLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-IN") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CINNERLabelState.DoExecute")
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CLabelCheckState.DoExecute...START")
        'MessageBox.Show("INNER_BARCD:" & task.ProcessText)

        Dim checkData As String = task.ProcessText
        'システム品番
        Dim sysitemCd As String = CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                            EnumBarcodeType.SYSITEM_CD)
        'ユニークコード
        Dim uniqueCd As String = CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                            EnumBarcodeType.UNIQUE_CD)
        '入り数
        Dim inBoxQty As String
        Dim hasuFlg As Boolean = False

        '端数の場合True
        '端数ラベルチェックロジック
        If CPersonal.HasuCheck(checkData) Then

            '端数入力画面
            Dim prevWindowMode As EnumWindowMode
            Dim result As String = ""

            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
            pdaTask.Hardware.alert()
            pdaTask.WindowManager.SetInputMessage(CConst.CNS_MSG_010, CConst.COLOR_BLACK, CConst.COLOR_GRAY)
            pdaTask.WindowManager.SetInputValue(0, CConst.COLOR_BLACK, CConst.COLOR_WHITE)
            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.INPUT_MODE)

            result = task.ReadTextWait(-1)
            inBoxQty = CInt(result)
            pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

            hasuFlg = True

        Else

            inBoxQty = CCommon.Change36(CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                                     EnumBarcodeType.INBOX_QTY))
        End If

        '社内ラベル照合
        Dim labelDataList As CCheck.LabelData = New CCheck.LabelData

        labelDataList.readText = checkData
        labelDataList.uniqueCd = uniqueCd
        labelDataList.empCd = pdaTask.EmpCd
        labelDataList.isSend = False
        labelDataList.actionDateTime = Now
        labelDataList.labelQty = inBoxQty      'ラベルにある入り数
        labelDataList.isFraction = hasuFlg '端数の場合はTrue以外はFalse

            'ラベルリストにADD
            ' 0 -> 成功
            ' 1 -> 数量オーバーの場合
            ' 2 -> orderCdが存在しない
            ' 3 -> 重複するuniqueCdがある場合
            ' 4 -> 品番が違い場合
        Select Case checkInfo.AddLabelData(pdaTask.OrderCd, sysitemCd, labelDataList)
            Case 0
                '成功
                Dim uniqueList As CCheck.UniqueCdData = New CCheck.UniqueCdData
                uniqueList.actionDateTime = Now
                uniqueList.empCd = pdaTask.EmpCd
                uniqueList.uniqueCd = uniqueCd

                'ユニークリスト追加
                If checkInfo.AddUniqueCdData(uniqueList) Then
                    Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                    setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                Else
                    '重複するuniqueCdがある場合
                    Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(labelDataList.uniqueCd)
                    Dim readYear As String = uniqueData.actionDateTime.ToString("yy")
                    Dim readMonth As String = uniqueData.actionDateTime.ToString("MM")
                    Dim readDay As String = uniqueData.actionDateTime.ToString("dd")
                    Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                    '統括コード
                    Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_018, readYear, readMonth, readDay, empInfo.empName)
                    ' 重複するuniqueCdがある場合
                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                          Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
                End If
            Case 1
                '数量オーバーする時
                Dim uniqueList As CCheck.UniqueCdData = New CCheck.UniqueCdData
                uniqueList.actionDateTime = Now
                uniqueList.empCd = pdaTask.EmpCd
                uniqueList.uniqueCd = uniqueCd

                'ユニークリスト追加
                If checkInfo.AddUniqueCdData(uniqueList) Then
                    Dim msgFlg As Boolean = AppManager.m_AppSetting.OverCountMsg
                    '数量オーバーする時には最初一回だけ
                    'エラー表示
                    If CCommon.FlagManage("overCount") And msgFlg Then
                        '数量オーバーの場合
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                     Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_019, Color.White)
                        CCommon.FlagManage("overCount", False)
                    End If

                    Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                    setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                Else
                    '重複するuniqueCdがある場合
                    Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(labelDataList.uniqueCd)
                    Dim readYear As String = uniqueData.actionDateTime.ToString("yy")
                    Dim readMonth As String = uniqueData.actionDateTime.ToString("MM")
                    Dim readDay As String = uniqueData.actionDateTime.ToString("dd")
                    Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                    '統括コード
                    Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_018, readYear, readMonth, readDay, empInfo.empName)
                    ' 重複するuniqueCdがある場合
                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                          Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
                End If

            Case 2
                'orderCdが存在しない
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_013, Color.White)
            Case 3
                '重複するuniqueCdがある場合
                Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(labelDataList.uniqueCd)
                Dim readYear As String = uniqueData.actionDateTime.ToString("yy")
                Dim readMonth As String = uniqueData.actionDateTime.ToString("MM")
                Dim readDay As String = uniqueData.actionDateTime.ToString("dd")
                Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                '統括コード
                Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_018, readYear, readMonth, readDay, empInfo.empName)
                ' 重複するuniqueCdがある場合
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
            Case 4
                '品番が違い場合
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_017, Color.White)

        End Select

        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.DoExit")
        Return True
    End Function


End Class
